package com.lanou.elh.sysmanage.authority_management.web.controller;

import com.lanou.elh.sysmanage.authority_management.common.Result;
import com.lanou.elh.sysmanage.authority_management.common.ResultUtil;
import com.lanou.elh.sysmanage.authority_management.entity.Log;
import com.lanou.elh.sysmanage.authority_management.service.LogService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


@RestController
public class LogController {
    @Autowired
    private LogService logService;

    @PostMapping("/user/selectAllLog")
    @ApiOperation(value = "查询全部log")
    public Result<List<Log>> selectAllLog(HttpServletRequest request){
        HttpSession session = request.getSession();
        System.out.println(session.getAttribute("token"));
        List<Log> selectAllLog = logService.selectAllLog();
        return new ResultUtil<List<Log>>().setData(selectAllLog);
    }

    @PostMapping("/admin/deleteOneLog")
    @ApiOperation(value = "删除其中一个log通过id")
    public Result deleteOneLog(Integer id){
        boolean b = logService.deleteOneLog(id);
        if (b==true){
            return new ResultUtil<>().setData(null);
        }else {
            return new ResultUtil<>().setErrorMsg("删除失败");
        }
    }

    @PostMapping("/admin/deleteManyLog")
    @ApiOperation(value = "同时删除多个log")
    public Result deleteManyLog(Integer[] id){
        logService.deleteManyLog(id);
        return new ResultUtil<>().setData(null);
    }

    @PostMapping("/user/selectLogByTerm")
    @ApiOperation(value = "对log进行模糊查询")
    public Result<List<Log>> selectLogByTerm(String term){
        List<Log> log = logService.selectLogByTerm(term);
        return new ResultUtil<List<Log>>().setData(log);
    }


}
